home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dist-packages / LanguageSelector / ImSwitch.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-10-12  |  4.8 KB  |  138 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from LocaleInfo import LocaleInfo
  5. import os.path as os
  6. import sys
  7. import subprocess
  8.  
  9. class ImSwitch(object):
  10.     global_confdir = '/etc/X11/xinit/xinput.d/'
  11.     local_confdir = os.path.expanduser('~/.xinput.d/')
  12.     bin = '/usr/bin/im-switch'
  13.     default_method = 'scim-bridge'
  14.     
  15.     def __init__(self):
  16.         pass
  17.  
  18.     
  19.     def available(self):
  20.         ''' return True if im-switch is available at all '''
  21.         return os.path.exists(self.bin)
  22.  
  23.     
  24.     def enabledForLocale(self, locale):
  25.         ''' check if we have a config for this specifc locale (e.g. ja_JP) '''
  26.         for dir in (self.local_confdir, self.global_confdir):
  27.             for name in (locale, 'all_ALL'):
  28.                 target = os.path.join(dir, name)
  29.                 if os.path.exists(target):
  30.                     im_name = os.path.basename(os.path.realpath(target))
  31.                     if im_name in ('none', 'default'):
  32.                         return False
  33.                     return True
  34.             
  35.         
  36.         return False
  37.  
  38.     
  39.     def enable(self, locale):
  40.         ''' enable input methods for locale'''
  41.         subprocess.call([
  42.             'im-switch',
  43.             '-z',
  44.             locale,
  45.             '-a'])
  46.         if not self.enabledForLocale(locale):
  47.             subprocess.call([
  48.                 'im-switch',
  49.                 '-z',
  50.                 locale,
  51.                 '-s',
  52.                 self.default_method])
  53.         
  54.  
  55.     
  56.     def disable(self, locale):
  57.         ''' disable input method for locale '''
  58.         if os.path.exists(os.path.join(self.local_confdir, locale)):
  59.             os.unlink(os.path.join(self.local_confdir, locale))
  60.         
  61.         if self.enabledForLocale(locale):
  62.             subprocess.call([
  63.                 'im-switch',
  64.                 '-z',
  65.                 locale,
  66.                 '-s',
  67.                 'none'])
  68.         
  69.  
  70.     
  71.     def getInputMethodForLocale(self, locale):
  72.         for dir in (self.local_confdir, self.global_confdir):
  73.             for name in (locale, 'all_ALL'):
  74.                 target = os.path.join(dir, name)
  75.                 if os.path.exists(target):
  76.                     return os.path.basename(os.path.realpath(target))
  77.             
  78.         
  79.  
  80.     
  81.     def getAvailableInputMethods(self):
  82.         ''' get the input methods available via im-switch '''
  83.         inputMethods = []
  84.         for dentry in os.listdir(self.confdir):
  85.             if not os.path.islink(self.confdir + dentry):
  86.                 inputMethods.append(dentry)
  87.                 continue
  88.         
  89.         inputMethods.sort()
  90.         return inputMethods
  91.  
  92.     
  93.     def setDefaultInputMethod(self, method, locale = 'all_ALL'):
  94.         ''' sets the default input method for the given locale
  95.             (in ll_CC form)
  96.         '''
  97.         l = self.confdir + locale
  98.         if os.path.islink(l):
  99.             os.unlink(l)
  100.         
  101.         os.symlink(self.confdir + method, l)
  102.         return True
  103.  
  104.     
  105.     def resetDefaultInputMethod(self, locale = 'all_ALL'):
  106.         ''' reset the default input method to auto (controlled by
  107.             im-switch
  108.         '''
  109.         d = '/etc/alternatives/xinput-%s' % locale
  110.         l = self.confdir + locale
  111.         if os.path.islink(l):
  112.             os.unlink(l)
  113.         
  114.         os.symlink(d, self.confdir + locale)
  115.         return True
  116.  
  117.     
  118.     def getCurrentInputMethod(self, locale = 'all_ALL'):
  119.         ''' get the current default input method for the selected
  120.             locale (in ll_CC form)
  121.         '''
  122.         return os.path.basename(os.path.realpath(self.confdir + locale))
  123.  
  124.  
  125. if __name__ == '__main__':
  126.     im = ImSwitch()
  127.     print im.getInputMethodForLocale('ja_JP')
  128.     print im.enabledForLocale('all_ALL')
  129.     sys.exit(1)
  130.     print 'available input methods: '
  131.     print im.getAvailableInputMethods()
  132.     print 'current method: ', im.getCurrentInputMethod()
  133.     print "switching to 'th-xim': ", im.setDefaultInputMethod('th-xim')
  134.     print 'current method: ', im.getCurrentInputMethod()
  135.     print 'reset default: ', im.resetDefaultInputMethod()
  136.     print 'current method: ', im.getCurrentInputMethod()
  137.  
  138.